using System.Threading;
using System.Threading.Tasks;
using UnityEngine;

namespace EntityKit.Runtime
{
    public static partial class AudioKit
    {
        /// <summary>
        /// 一次性音效音量
        /// </summary>
        public static float OneShootVolume
        {
            get => oneShootVolume;
            set
            {
                if (oneShootSource != null) oneShootSource.volume = oneShootVolume = Mathf.Clamp01(value);
            }
        }

        public static bool OneShootMute
        {
            get => isOneShootMute;
            set
            {
                if (isOneShootMute == value) return;
                isOneShootMute = value;
                if (oneShootSource != null) oneShootSource.mute = value;
            }
        }

        /// <summary>
        /// 播放一次性音效
        /// </summary>
        /// <param name="clip">音频剪辑</param>
        public static void PlayOneShootSound(AudioClip clip)
        {
            if (!isInitialized || !ValidateClip(clip)) return;
            oneShootSource?.PlayOneShot(clip, oneShootVolume);
        }


        private static void OneShootSoundInitialize()
        {
            oneShootSource = CreateAudioSource("OneShootSource", oneShootVolume, 1, 0, root, isOneShootMute);
            if (oneShootSource != null)
            {
                OneShootVolume = oneShootVolumeDefault;
            }
        }

        private static void OneShootSoundUpdate()
        {
        }

        private static void OneShootSoundRelease()
        {
            if (oneShootSource != null)
            {
                Object.Destroy(oneShootSource.gameObject);
                oneShootSource = null;
            }
        }

        private static AudioSource oneShootSource;

        private static float oneShootVolumeDefault = 1;

        private static float oneShootVolume;

        private static bool isOneShootMute;
    }
}